//
//  QXPopoverPC.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/27.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit

class QXPopoverPC: UIPresentationController {

    var prestentFrame = CGRectZero
    var ispresented = false
    
    init(prestentFrame: CGRect) {
        super.init()
        self.prestentFrame = prestentFrame
    }
    
    lazy var maskBtnView: UIButton = {
       let mbv = UIButton()
        mbv.backgroundColor = UIColor.clearColor()
        mbv.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        return mbv
    }()
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        maskBtnView.frame = containerView!.bounds
        containerView?.insertSubview(maskBtnView, atIndex: 0)
        presentedView()?.frame = prestentFrame
    }
    
    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}

extension QXPopoverPC: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    // 返回动画代理对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = QXPopoverPC(presentedViewController: presented, presentingViewController: presenting)
        pc.prestentFrame = prestentFrame
        return pc
        
    }
    
    // 返回动画代理对象，并配置已经展现的控制器
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        ispresented = true
        return self
    }
    
    // 返回动画代理对象，并配置将要展现的控制器
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        ispresented = false
        return self
    }
    
    // 返回动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1.2
    }
    
    // 执行动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        // 出场
        if ispresented {
            
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            transitionContext.containerView()?.addSubview(toView)
            
            toView.alpha = 0
            UIView.animateWithDuration(0, animations: { () -> Void in
                toView.alpha = 1.0
                transitionContext.completeTransition(true)
            })

        // 消失
        } else {
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
  
            fromView.removeFromSuperview()
            transitionContext.completeTransition(true)
            
            // 注，消失的动画实现的时候，经常会突然消失，有同学发现是因为 0 在作怪，改成 0。00001有意想不到的效果
  
        }
        
    }
    
}
